<?php
	include('header.php');
?>		
<script>
	$(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		var name = $( "#name" ),
			mapTransport = new Object(),
			arrTransport,
			idReceiver = $( "#idReceiver" ), 
		    tpdID = $( "#tpdID" ),
			postcode = $( "#postcode" ),
			addpostcode = $( "#addpostcode" ),
			city = $( "#city" ),
			state = $( "#state" ),
			addname = $( "#addname" ),
			allFields = $( [] ).add( name ).add( postcode ).add( city ).add( state ).add( addpostcode ).add( addname ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Length of " + n + " must be between " +
					min + " and " + max + "." );
				return false;
			} else {
				return true;
			}
		}
		
		function errorField( o, n ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#dialog-addreceiver" ).dialog({
			autoOpen: false,
			height: 450,
			width: 350,
			modal: true,
			buttons: {
				"Create a receiver": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( addname, "Name", 3, 150 );
					bValid = bValid && checkLength( addpostcode, "Postcode", 5, 5 );
				
					//bValid = bValid && checkRegexp( addname, /^[a-z]([0-9a-z_])+$/i, "Name may consist of a-z, 0-9, underscores, begin with a letter." );
					bValid = bValid && checkRegexp( addpostcode, /^[0-9]([0-9_])+$/i, "Postcode may consist of 0-9." );
					
					$('#loading').show();  
					var data = 'tpdID='+tpdID.val()+'&name='+addname.val()+'&postcode='+addpostcode.val();
					$.ajax({
						url: "ajax/apostcode.php",  
						type: "GET",        
						data: data,     
						cache: false,
						success: function (res) {  
							if(res == 1){
								location.reload();
								//window.location = document.URL;
							}
							//hide the progress bar
							$('#loading').hide();          
						}       
					});
					
					
					if ( bValid ) {
						$( "#users tbody" ).append( "<tr>" +
							"<td>" + addname.val() + "</td>" + 
							"<td>" + addpostcode.val() + "</td>" + 
						"</tr>" ); 
						$( this ).dialog( "close" );
					}
					
					
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});

		$( "#dialog-updTransport" ).dialog({
			autoOpen: false,
			height: 100,
			width: 350,
			modal: true,
			buttons: {
				"assign a transport": function() {
					$('#loading').show();  
					var transportdd = $('#transportdd :selected').val();
					var data = 'listid='+arrTransport+'&transportid='+transportdd;
					$.ajax({
						url: "ajax/atransport.php",  
						type: "GET",        
						data: data,     
						cache: false,
						success: function (res) {  
							if(res == 1){
								alert('Success Assign Transport!');
								location.reload();
								//window.location = document.URL;
							}
							//hide the progress bar
							$('#loading').hide();          
						}       
					});
					
					$( this ).dialog( "close" );
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		
		$( "#dialog-updPostcode" ).dialog({
			autoOpen: false,
			height: 350,
			width: 350,
			modal: true,
			buttons: {
				"update a receiver": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );
					
					bValid = bValid && checkLength( postcode, "postcode", 5, 5 );
					bValid = bValid && checkRegexp( postcode, /^[0-9]([0-9_])+$/i, "Postcode may consist of 0-9." );
					
			
					$('#loading').show();  
					var data = 'recID='+idReceiver.val()+'&postcode='+$( "#postcode" ).val();
					$.ajax({
						url: "ajax/upostcode.php",  
						type: "GET",        
						data: data,     
						cache: false,
						success: function (res) {  
							if(res == 1){
								location.reload();
								//window.location = document.URL;
							}
							//hide the progress bar
							$('#loading').hide();          
						}       
					});
					
					if ( bValid ) {
						$( "#users tbody" ).append( "<tr>" +
							"<td>" + postcode.val() + "</td>" + 
						"</tr>" ); 
						$( this ).dialog( "close" );
					}
					
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		
		$("button[id^=addreceiver]").button()
			.click(function() {
				$( "#dialog-addreceiver" ).dialog( "open" );
				tips.text('Fill in name and postcode field only. Postcode field must start from 7.');
				
				var tpdIDBut = this.id;
				var tpdIDSplit = tpdIDBut.split("-");
				$('#tpdID').val(tpdIDSplit[1]);

				
		});
		$("button[id^=updPostcode]").button()
			.click(function() {	
				$( "#dialog-updPostcode" ).dialog( "open" );
				tips.text('Fill in postcode field only. Postcode field must start from 7.');
				var recIdBut = this.id;
				var recIdSplit = recIdBut.split("-");

				var data = 'recID='+recIdSplit[1];
				$('#loading').show();  
				$.ajax({
					url: "ajax/receiver.php",  
					type: "GET",        
					data: data,     
					cache: false,
					success: function (html) {  
					 
						//hide the progress bar
						$('#loading').hide();   
						 
						//add the content retrieved from ajax and put it in the #content div
						var nameEnt = html.split("$");
						$('#name').val(nameEnt[0]);
						$('#idReceiver').val(recIdSplit[1]);
						   
					}       
				});
		});
		$("input[id^=cbtransport]").click(function() {	
		
			var trnIDSplit = this.id.split("-");
			var transportID = trnIDSplit[1];
			mapTransport[transportID] = transportID;
			arrTransport= $.map(mapTransport, function(value, index) {
				return [[value]];
			});

		});
		$("#searchnamebtn").button()
			.click(function() {	
				var searchname = $('#searchname').val();
				$('#loading').show();  
				var data = 'searchname='+searchname;
				$.ajax({
					url: "ajax/receiver.php",  
					type: "GET",        
					data: data,     
					cache: false,
					success: function (result) {  
						//hide the progress bar
						$('#loading').hide();   
						allFields.removeClass( "ui-state-error" );
						if(result != ''){
							var nameEnt = result.split("$");
							$('#addname').val(nameEnt[0]);
							if(nameEnt[1] != ""){
								var addr = nameEnt[1];
								var addrSplit = addr.split("-");
								$('#addpostcode').val(addrSplit[0]);
								$('#addcity').val(addrSplit[1]);
								$('#addstate').val(addrSplit[2]); 
							}
							
						}else {
							$('#addname').val('');
							alert('Search for name not found!');
							
						}
					}       
				});
				
				return false;
			
		});
		
		$("#updateTransport").button()
			.click(function() {
				$("#dialog-updTransport" ).dialog( "open" );
				
				$.getJSON("ajax/transport.php",{ajax: 'true'}, function(j){
					  var options = '';
					  for (var i = 0; i < j.length; i++) {
						
						options += '<option value="' + j[i].optionValue + '" ' + j[i].optionSelected + '>' + j[i].optionDisplay + '</option>';
					  }
					  $("select#transportdd").html(options);
					  
				})

		});
		
		$('#addpostcode').bind('keyup paste click', function()  {
			$('#addcity').val('');
			$('#addstate').val('');  
			
			var pcode = $('#addpostcode').val();
			var charFirst = pcode.substr(0, 1);

			if(charFirst != '7' ){
				$('#addpostcode').val('7');
			}
				
			var num = $('#addpostcode').val().length;
			var num = $('#addpostcode').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			var intRegex = /^\d+$/;
			if(num.length>=5) {
				var data = 'postcode='+num;
				$('#loading').show();  
				$.ajax({
					url: "ajax/postcode.php",  
					type: "GET",        
					data: data,     
					cache: false,
					success: function (result) {  
						//hide the progress bar
						$('#loading').hide();   
						allFields.removeClass( "ui-state-error" );
						tips.text('Fill in name and postcode field only. Postcode field must start from 7.');
						if(result == ','){
							var bValid = true;
							
							
							bValid = bValid && errorField( addpostcode, "Postcode not found! Re-type another postcode" );
							if ( bValid ) {
								allFields.val( "" ).removeClass( "ui-state-error" );
							}
						}else {
							var  resultSplit = result.split(",");
							$('#addcity').val(resultSplit[0]);
							$('#addstate').val(resultSplit[1]);   
						}
					}       
				});
			}
		});
		
		$('#postcode').bind('keyup paste click', function()  {
			$('#city').val('');
			$('#state').val('');  
			
			var pcode = $('#postcode').val();
			var charFirst = pcode.substr(0, 1);

			if(charFirst != '7' ){
				$('#postcode').val('7');
			}
				
			var num = $('#postcode').val().length;
			var num = $('#postcode').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			var intRegex = /^\d+$/;
			if(num.length>=5) {
				var data = 'postcode='+num;
				$('#loading').show();  
				$.ajax({
					url: "ajax/postcode.php",  
					type: "GET",        
					data: data,     
					cache: false,
					success: function (result) {  
						//hide the progress bar
						$('#loading').hide();   
						allFields.removeClass( "ui-state-error" );
						tips.text('Fill in postcode field only. Postcode field must start from 7.');
						if(result == ','){
							var bValid = true;
							
							
							bValid = bValid && errorField( postcode, "Postcode not found! Re-type another postcode" );
							if ( bValid ) {
								
								allFields.val( "" ).removeClass( "ui-state-error" );
							}
						}else {
							var  resultSplit = result.split(",");
							$('#city').val(resultSplit[0]);
							$('#state').val(resultSplit[1]);   
						}
					}       
				});
			}
		});
	
	});
	</script>
	<div id="dialog-addreceiver" title="Create new receiver">
		<p class="validateTips">Fill in name and postcode field only. Postcode field must start from 7.</p>

		<form>
		<span id="loading" style="display:none;" >loading.....</span>
		<fieldset>
			<label for="searchname">Search Name</label>
			<input type="text" name="searchname" id="searchname" class="text ui-widget-content ui-corner-all" />
			<button id='searchnamebtn'>Search Exact Name</button>
			<label>&nbsp;</label>
			
			<label for="addname">Name</label>
			<input type="text" name="addname" id="addname" class="text ui-widget-content ui-corner-all" />
			<label for="addpostcode">Postcode</label>
			<input type="text" name="addpostcode" id="addpostcode" value="" class="text ui-widget-content ui-corner-all" />
			<label for="addcity">City</label>
			<input type="text" name="addcity" id="addcity" value="" class="text ui-widget-content ui-corner-all" readonly style="background:#f2f2f2;"/>
			<label for="addstate">State</label>
			<input type="text" name="addstate" id="addstate" value="" class="text ui-widget-content ui-corner-all" readonly style="background:#f2f2f2;"/>
		</fieldset>
		<input type="hidden" name="tpdID" id="tpdID"  />
		</form>
	</div>
	
	<div id="dialog-updPostcode" title="Update receiver">
		<p class="validateTips">Fill in postcode field only. Postcode field must start from 7.</p>

		<form>
		<span id="loading" style="display:none;">loading.....</span>
		<fieldset>
			<label for="name">Name</label>
			<input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" readonly  style="background:#f2f2f2;"/>
			<label for="postcode">Postcode</label>
			<input type="text" name="postcode" id="postcode" value="" class="text ui-widget-content ui-corner-all" />
			<label for="city">City</label>
			<input type="text" name="city" id="city" value="" class="text ui-widget-content ui-corner-all" readonly  style="background:#f2f2f2;"/>
			<label for="state">State</label>
			<input type="text" name="state" id="state" value="" class="text ui-widget-content ui-corner-all" readonly  style="background:#f2f2f2;"/>
		</fieldset>
		<input type="hidden" name="idReceiver" id="idReceiver"  />
		</form>
	</div>
	
	<div id="dialog-updTransport" title="Assign transport">
		<p class="validateTips">Please select a transport.</p>

		<form>
		<span id="loading" style="display:none;">loading.....</span>
		<fieldset>
			<label for="name">Transport</label>
			<select id="transportdd" name="transportdd" class="select ui-widget-content ui-corner-all" style="height:20px; width: 99%;">
				<option> </option>
			</select>
		</fieldset>
		</form>
	</div>
	

	

		<!--[if !IE]>start content<![endif]-->					
		<div id="content">
			<div class="inner">
				
				<!--[if !IE]>start section<![endif]-->
				<?php

				

				?>
		
				<div class="section">
					
					<!--[if !IE]>start title wrapper<![endif]-->
					<div class="title_wrapper">
						<span class="title_wrapper_top"></span>
						<div class="title_wrapper_inner">
							<span class="title_wrapper_middle"></span>
							<div class="title_wrapper_content">
								<h2>List Package:</h2>
							</div>
						</div>
						<span class="title_wrapper_bottom"></span>
					</div>
					<!--[if !IE]>end title wrapper<![endif]-->
					
					<!--[if !IE]>start section content<![endif]-->
					<div class="section_content">
						<span class="section_content_top"></span>
						
						<div class="section_content_inner">
							<?php
								 
								$notdn = "";  
								$filter = "";
								if(isset($_POST['reset'])){
									$_POST = "";
								}
								if(isset($_GET['filter'])){ 
									$filter = mysql_real_escape_string($_GET['filter']); 
								}
								if(isset($_POST['filter'])){ 
									$filter = mysql_real_escape_string($_POST['filter']); 
								}
								
								if(isset($_GET['notdn'])){ 
									$notdn = mysql_real_escape_string($_GET['notdn']); 
								}
								if(isset($_POST['notdn'])){ 
									$notdn = mysql_real_escape_string($_POST['notdn']); 
								}
								if($notdn != '') {
									$filter = '';
								}
								
								//default
								$limit = 10; 
								if(isset($_GET['limit'])){ 
									$limit = mysql_real_escape_string($_GET['limit']); 
								}
								if(isset($_POST['limit'])){ 
									$limit = mysql_real_escape_string($_POST['limit']); 
								}
								
								//use for pagination url parameter
								$param = "&filter=$filter&limit=$limit";
							?>
							<form action="package.php" method="POST" class="search_form">
							
							
							<!--[if !IE]>start fieldset<![endif]-->
							<fieldset>
								<!--[if !IE]>start forms<![endif]-->
								<div class="forms">
								<h3>Search : <?php  echo $notdn; ?> </h3>
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>TDN NO. :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
											<input class="text" name="notdn" value="<?php //if($notdn != "") echo $notdn; ?>" type="text" />
										</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>Filter By :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
										<select name="filter" >
											<option value="" <?php if($filter=='') echo 'selected'; ?> ></option>
											<option value="01" <?php if($filter=='01') echo 'selected'; ?>>Complete Info</option>
											<option value="02" <?php if($filter=='02') echo 'selected'; ?>>Missing Postcode By Receiver</option>
											<option value="03" <?php if($filter=='03') echo 'selected'; ?>>Missing Receiver</option>
										</select>
										
									</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>Display Records Per Page :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
										<select name="limit" >
											<option value="10" <?php if($limit=='10') echo 'selected'; ?> >10</option>
											<option value="30" <?php if($limit=='30') echo 'selected'; ?> >30</option>
											<option value="50" <?php if($limit=='50') echo 'selected'; ?> >50</option>
											<option value="100" <?php if($limit=='100') echo 'selected'; ?> >100</option>
										</select>
										
									</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<div class="inputs">
										<span class="button blue_button search_button"><span><span><em>SEARCH</em></span></span><input name="cari" type="submit" /></span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								</div>
								<!--[if !IE]>end forms<![endif]-->
								
							</fieldset>
							<!--[if !IE]>end fieldset<![endif]-->
							
							
							</form>
							<br />
							<?php
								/* 
									   First get total number of rows in data table. 
									   If you have a WHERE clause in your query, make sure you mirror it here.
									*/
									
									
									if($filter == '01'){
										$addClause = " AND LENGTH(r.ent_short_name) > 2 AND r.ent_add_postcode is not null AND  r.ent_add_postcode != ''";
									}
									if($filter == '02'){
										$addClause = " AND LENGTH(r.ent_short_name) > 2 AND r.ent_short_name != '<NOT AVAILABLE>'  AND (r.ent_add_postcode = '' OR r.ent_add_postcode is null) "; 
									}
									if($filter == '03'){
										$addClause = " AND (LENGTH(r.ent_short_name) < 3 OR r.ent_short_name = '<NOT AVAILABLE>') "; 
									}
									
									if($notdn != ''){
										$addClause = " AND tpd_tdn_no = '$notdn' ";
									}
									
									$qCount= "SELECT COUNT(tpd_id) FROM transport_package_delivering tpd, entity s, entity r
												WHERE tpd.tpd_sender_ent_id=s.ent_id
												AND tpd.tpd_receiver_ent_id=r.ent_id
												AND tpd.tpd_trn_id IS NULL
												$addClause 
												ORDER BY r.ent_add_postcode desc";
									$total_pages = $db->sql_total($qCount);
									
									if($total_pages > 0){
										echo '<h3>'.$total_pages.' '.$lang['CONTENT_RECORDS'].'</h3>';
									}
									/* Setup vars for query. */
									//$limit = 10; 								//how many items to show per page
							
							?>
							<!--[if !IE]>start table_wrapper<![endif]-->
							<div class="table_wrapper">
								<div class="table_wrapper_inner">
								
								<?php  
									$page="";
									if(isset($_GET['page']))
										$page=$_GET['page'];
									
									if($page) 
										$start = ($page - 1) * $limit; 			//first item to display on this page
									else
										$start = 0;								//if no page var is given, set start to 0
									
									/* Get data. */
									$qList= "SELECT tpd_id,tpd_sender_ent_id,tpd_receiver_ent_id,tpd_tdn_no,tpd_do_ref,tpd_qty,tpd_qty_type,r.ent_add_postcode as postcode, r.ent_add_city as city,r.ent_short_name as receiver,s.ent_short_name as sender FROM transport_package_delivering tpd, entity s, entity r
												WHERE tpd.tpd_sender_ent_id=s.ent_id
												AND tpd.tpd_receiver_ent_id=r.ent_id
												AND tpd.tpd_trn_id IS NULL
												$addClause 
												ORDER BY r.ent_add_postcode DESC
												 LIMIT $start, $limit ";
									$rList = $db->sql_fetch($qList);			

								?>
								<table cellpadding="0" cellspacing="0" width="100%">
									<tbody>
									<tr>
										<th style="width: 10px;">Bil.</th>
										<th style="width: 50px;">TDN No.</th>
										<th style="width: 50px;">Quatity</th>
										<th>Sender</th>
										<th>Receiver</a></th>
										<th>Send To</a></th>
										<th style="width: 80px;">Assign Transport</a></th>
										
									</tr>
								<?php
									$num =$start;
									foreach ($rList as $i) {
										$num++;
										$colorRow = "first";
										if($num%2==0)
											$colorRow = "second"; 
										else
											$colorRow = "first";
										
										$receiver = $i['receiver'];
										$add = "";
										$assignLorry="";
										if($i['receiver']=='<NOT AVAILABLE>' || $i['receiver']=='' || $i['receiver']==null || (strlen($i['receiver']) < 3) ){
											$receiver = "<button id='addreceiver-$i[tpd_id]'>Add Receiver</button>";
											$add = "N/A";
											$assignLorry="N/A";
										}else{
											if($i['postcode'] == null || $i['postcode'] == ""){
												$add = "<button id='updPostcode-$i[tpd_receiver_ent_id]'>Update Postcode</button>";
												$assignLorry="N/A";
											}else{
												$add = $i['postcode'].', '.$i['city'];
												$assignLorry="<input type='checkbox' id='cbtransport-$i[tpd_id]'  />";
											}
										}
										echo '	
											<tr class="'.$colorRow.'">
												<td>'.$num.'</td>
												<td>'.$i['tpd_tdn_no'].'</td>
												<td>'.$i['tpd_qty'].' '.$i['tpd_qty_type'].'</td>
												<td>'.$i['sender'].'</td>
												<td>'.$receiver.'</td>
												<td>'.$add.'</td>
												<td>'.$assignLorry.'</td>
											</tr>';
										
									}
								?>
								</tbody></table>
								</div>
								<span style="float:right; margin-top:5px;" >
									<button id='updateTransport'>Update Transport</button>
								<span>
							</div>
							
							<!--[if !IE]>end table_wrapper<![endif]-->
							
						</div>
						
						<!--[if !IE]>start pagination<![endif]-->
							<div class="pagination_wrapper">
							<span class="pagination_top"></span>
							<div class="pagination_middle">
							<div class="pagination">
								<?php
								// How many adjacent pages should be shown on each side?
									$adjacents = 3;
								/* Setup page vars for display. */
									if ($page == 0) $page = 1;					//if no page var is given, default to 1.
									$prev = $page - 1;							//previous page is page - 1
									$next = $page + 1;							//next page is page + 1
									$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
									$lpm1 = $lastpage - 1;						//last page minus 1
								/* 
									Now we apply our rules and draw the pagination object. 
									We're actually saving the code to a variable in case we want to draw it more than once.
								*/
								echo '<span class="page_no">'.$lang['CONTENT_PAGINATION_PAGE'].' '.$page.' of '.$lastpage.' </span>';
								echo '<ul class="pag_list">';

									//previous button
									if ($page > 1) 
										echo '<li><a href="package.php?page='.$prev.$param.'" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_PREVIOUS'].'</span></span></a> </li>';
									else
										echo '<li><a href="#" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_PREVIOUS'].'</span></span></a> </li>';

									//pages	
									if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
									{	
										for ($counter = 1; $counter <= $lastpage; $counter++)
										{
											if ($counter == $page)
												echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
											else	
												echo '<li><a href="package.php?page='.$counter.$param.'">'.$counter.'</a></li>';	
										}
									}
									elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
									{
										//close to beginning; only hide later pages
										if($page < 1 + ($adjacents * 2))		
										{
											for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else	
													echo '<li><a href="package.php?page='.$counter.$param.'">'.$counter.'</a></li>';													
											}
											echo '<li>[...]</li>';
											echo '<li><a href="package.php?page='.$lpm1.$param.'">'.$lpm1.'</a></li>';	
											echo '<li><a href="package.php?page='.$lastpage.$param.'">'.$lastpage.'</a></li>';		
										}
										//in middle; hide some front and some back
										elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
										{
											echo '<li><a href="package.php?page=1'.$param.'">1</a></li>';	
											echo '<li><a href="package.php?page=2'.$param.'">2</a></li>';
											echo '<li>[...]</li>';											
											for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else
													echo '<li><a href="package.php?page='.$counter.$param.'">'.$counter.'</a></li>';						
											}
											echo '<li>[...]</li>';
											echo '<li><a href="package.php?page='.$lpm1.$param.'">'.$lpm1.'</a></li>';	
											echo '<li><a href="package.php?page='.$lastpage.$param.'">'.$lastpage.'</a></li>';												
										}
										//close to end; only hide early pages
										else
										{
											echo '<li><a href="package.php?page=1'.$param.'">1</a></li>';	
											echo '<li><a href="package.php?page=2'.$param.'">2</a></li>';
											echo '<li>[...]</li>';	
											for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else
													echo '<li><a href="package.php?page='.$counter.$param.'">'.$counter.'</a></li>';						
											}
										}
									}
									//next button
									if ($page < $counter - 1) 
										echo '<li><a href="package.php?page='.$next.$param.'" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_NEXT'].'</span></span></a> </li>';
									else
										echo '<li><a href="#" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_NEXT'].'</span></span></a> </li>';
									?>
								</ul>
							</div>
							</div>
							<span class="pagination_bottom"></span>
							</div>
						<!--[if !IE]>end pagination<![endif]-->
						
						
						<span class="section_content_bottom"></span>
					</div>
					<!--[if !IE]>end section content<![endif]-->
				</div>
				<!--[if !IE]>end section<![endif]-->
				
					
				
			</div>
		</div>
		<!--[if !IE]>end content<![endif]-->

<?php
	include('footer.php');
?>